package com.example.miniappssm.service.serviceImpl;

import com.example.miniappssm.bean.GrowthNotes;
import com.example.miniappssm.bean.GrowthRecords;
import com.example.miniappssm.mapper.GrowthMapper;
import com.example.miniappssm.service.GrowthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GrowthServiceImpl implements GrowthService {

    @Autowired
    GrowthMapper growthMapper;

    /**
     * 在主页展示农作物记录列表
     * @author codertao
     * @date 2022/5/21 8:53
     * @return java.util.List<com.example.miniappssm.bean.GrowthNotes>
     */
    @Override
    public List<GrowthNotes> selectAllGrowthNotes() {
        return growthMapper.selectAllGrowthNotes();
    }

    /**
     * 新增一个新的农作物记录
     * @author codertao
     * @date 2022/5/21 8:52
     */
    @Override
    public void addNewCropNote(GrowthNotes growthNotes) {

        growthMapper.insertGrowthNotes(growthNotes);
        GrowthRecords growthRecords = new GrowthRecords();
        growthRecords.setCropId(growthNotes.getCropId());
        growthRecords.setRecordDate(growthNotes.getCropDate());
        growthRecords.setRecordMain("播种了" + growthNotes.getCropName());
        growthRecords.setRecordImg(growthNotes.getCropImg());
        growthMapper.insertGrowthRecords(growthRecords);
    }

    /**
     * 根据农作物查询记录列表
     * @author codertao
     * @date 2022/5/21 9:59
     * @return java.util.List<com.example.miniappssm.bean.GrowthRecords>
     */
    @Override
    public List<GrowthRecords> selectAllGrowthRecordByCropId(Integer cropId) {
        return growthMapper.selectAllGrowthRecordByCropId(cropId);
    }

    /**
     * 新增一个生长记录并且更新农作物的封面图片
     * @author codertao
     * @date 2022/5/21 10:28
     */
    @Override
    public void addNewCropRecord(GrowthRecords growthRecords) {
        growthMapper.insertGrowthRecords(growthRecords);
        growthMapper.updateCropImgByCropId(growthRecords.getCropId(),growthRecords.getRecordImg());
    }

    @Override
    public GrowthNotes selectGrowthNotesByGrowthCropId(Integer cropId) {
        return growthMapper.selectGrowthNotesByCropId(cropId);
    }

    @Override
    public void updateGrowthNotes(GrowthNotes growthNotes) {
        growthMapper.updateGrowthNotes(growthNotes);
    }
}
